package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.Loger;
import com.iknow.util.Regex;
import com.iknow.util.StringUtil;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import com.xmpp.task.LoginAsyncTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginActivity extends Activity
{
  private static final Intent SERVICE_INTENT = new Intent();
  private View.OnClickListener LoginButtonClickListener;
  private View.OnClickListener LoginTipCliclListener;
  private boolean mBinded;
  private XmppServiceConnection mConn;
  private AutoCompleteTextView mEmailEditText;
  private LoginAsyncTask mLoginTask;
  private LinearLayout mLoginTip;
  private ProgressDialog mProgressDialog;
  private EditText mPwdEdit;
  private Button mRegisterButton;
  private TaskListener mTaskListener;
  private TextView mTitle;
  private IXmppFacade mXmppFacade;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
    SERVICE_INTENT.setAction("login_start");
  }

  public LoginActivity()
  {
    this.mConn = new XmppServiceConnection(this, null);
    this.LoginButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (!(IKnow.IsNetAviable()))
          Toast.makeText(this.this$0, this.this$0.getResources().getString(2131361878), 0).show();
        while (true)
        {
          label149: 
          do
            while (true)
            {
              String str1;
              while (true)
              {
                do
                  return;
                while ((LoginActivity.access$0(this.this$0) != null) && (LoginActivity.access$0(this.this$0).getStatus() == AsyncTask.Status.RUNNING));
                str1 = LoginActivity.access$1(this.this$0).getText().toString();
                if (str1.equalsIgnoreCase("im_qplus"))
                  break label149;
                if (!(StringUtil.isEmpty(str1)))
                  break;
                Toast.makeText(this.this$0, "请先填写邮箱地址", 0).show();
              }
              String str2 = str1.trim();
              Matcher localMatcher = Regex.EMAIL_ADDRESS_PATTERN.matcher(str2);
              if ((StringUtil.isEmpty(str2)) || (localMatcher.matches()))
                break;
              Toast.makeText(this.this$0, "请先填写邮箱地址", 0).show();
            }
          while (LoginActivity.access$2(this.this$0));
          LoginActivity.access$5(this.this$0, this.this$0.bindService(LoginActivity.access$3(), LoginActivity.access$4(this.this$0), 1));
        }
      }
    };
    this.LoginTipCliclListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        Intent localIntent = new Intent(this.this$0, RegisterActivity.class);
        this.this$0.startActivity(localIntent);
        this.this$0.finish();
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "UserInfo";
      }

      public void onCancelled()
      {
        Loger.i("LoginTask", "LoginTask is cancel!!");
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
          LoginActivity.access$7(this.this$0);
        while (true)
        {
          return;
          LoginActivity.access$8(this.this$0, ((LoginAsyncTask)paramGenericTask).getErrorMessage());
        }
      }

      public void onPreExecute()
      {
        LoginActivity.access$6(this.this$0);
      }

      public void onProgressUpdate(, Object paramObject)
      {
      }
    };
  }

  private void initView()
  {
    this.mEmailEditText = ((AutoCompleteTextView)findViewById(2131296279));
    this.mRegisterButton = ((Button)findViewById(2131296278));
    this.mRegisterButton.setOnClickListener(this.LoginButtonClickListener);
    this.mPwdEdit = ((EditText)findViewById(2131296280));
    this.mEmailEditText.addTextChangedListener(new EmailTextWatcher(this, null));
    this.mLoginTip = ((LinearLayout)findViewById(2131296422));
    this.mLoginTip.setOnClickListener(this.LoginTipCliclListener);
    this.mTitle = ((TextView)findViewById(2131296343));
    this.mTitle.setText("登录");
    this.mTitle.setVisibility(0);
    findViewById(2131296342).setVisibility(8);
    findViewById(2131296344).setVisibility(8);
  }

  private void login()
  {
    this.mLoginTask = new LoginAsyncTask();
    this.mLoginTask.setListener(this.mTaskListener);
    TaskParams localTaskParams = new TaskParams();
    localTaskParams.put("xmpp", this.mXmppFacade);
    String str1 = this.mEmailEditText.getText().toString();
    if (!(str1.equalsIgnoreCase("im_qplus")))
      str1 = StringUtil.getMD5String(this.mEmailEditText.getText().toString());
    String str2 = this.mPwdEdit.getText().toString();
    if (StringUtil.isEmpty(str2))
      str2 = "111111";
    localTaskParams.put("jid", str1);
    localTaskParams.put("password", str2);
    localTaskParams.put("email", this.mEmailEditText.getText().toString());
    LoginAsyncTask localLoginAsyncTask = this.mLoginTask;
    TaskParams[] arrayOfTaskParams = new TaskParams[1];
    arrayOfTaskParams[0] = localTaskParams;
    localLoginAsyncTask.execute(arrayOfTaskParams);
  }

  private void onLoginBegin()
  {
    this.mProgressDialog = ProgressDialog.show(this, "", "正在登录，请稍等", true);
  }

  private void onLoginFailure(String paramString)
  {
    if (this.mBinded)
    {
      unbindService(this.mConn);
      this.mBinded = false;
    }
    if (this.mProgressDialog != null)
      this.mProgressDialog.dismiss();
    Toast.makeText(this, paramString, 0).show();
  }

  private void onLoginSuccess()
  {
    String str1 = StringUtil.getMD5String(this.mEmailEditText.getText().toString());
    String str2 = this.mPwdEdit.getText().toString();
    if (StringUtil.isEmpty(str2))
      str2 = "111111";
    IKnow.mSystemConfig.setString("account_username", str1 + "@" + "imiknow.com");
    IKnow.mSystemConfig.setString("account_password", str2);
    startService(SERVICE_INTENT);
    if (this.mProgressDialog != null)
      this.mProgressDialog.dismiss();
    Toast.makeText(this, "登录成功", 0).show();
    finish();
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    setContentView(2130903080);
    initView();
  }

  protected void onDestroy()
  {
    if (this.mBinded)
    {
      unbindService(this.mConn);
      this.mBinded = false;
    }
    super.onDestroy();
  }

  protected void onPause()
  {
    super.onPause();
  }

  protected void onResume()
  {
    super.onResume();
  }

  private class EmailTextWatcher
  implements TextWatcher
  {
    ArrayAdapter<String> adapt = null;

    public void afterTextChanged()
    {
    }

    public void beforeTextChanged(, int paramInt1, int paramInt2, int paramInt3)
    {
    }

    public void onTextChanged(, int paramInt1, int paramInt2, int paramInt3)
    {
      String str1 = paramCharSequence.toString();
      if (StringUtil.isEmpty(str1));
      while (true)
      {
        String str2;
        do
        {
          return;
          str2 = str1.substring(paramInt1);
        }
        while ((StringUtil.isEmpty(str2)) || (!(str2.equalsIgnoreCase("@"))));
        String[] arrayOfString = new String[7];
        arrayOfString[0] = str1 + "163.com";
        arrayOfString[1] = str1 + "sina.com";
        arrayOfString[2] = str1 + "qq.com";
        arrayOfString[3] = str1 + "126.com";
        arrayOfString[4] = str1 + "gmail.com";
        arrayOfString[5] = str1 + "hotmail.com";
        arrayOfString[6] = str1 + "sohu.com";
        this.adapt = new ArrayAdapter(this.this$0, 17367050, arrayOfString);
        LoginActivity.access$1(this.this$0).setAdapter(this.adapt);
        LoginActivity.access$1(this.this$0).setThreshold(paramInt1);
      }
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      LoginActivity.access$9(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
      LoginActivity.access$10(this.this$0);
    }

    public void onServiceDisconnected()
    {
      LoginActivity.access$9(this.this$0, null);
    }
  }
}